package cc.zstart.utils;

import org.apache.commons.collections.MapUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;

public class MapUtil extends MapUtils {

    @SuppressWarnings("unchecked")
    public static Map<Object, Object> objObj(Object... objs) {
        return MapUtils.putAll(new HashMap<Object, Object>(), objs);
    }

    @SuppressWarnings("unchecked")
    public static Map<String, Object> strObj(Object... objs) {
        if (objs.length % 2 != 0) {
            throw new RuntimeException("长度必须是2的倍数");
        }
        for (int i = 0; i < objs.length; ) {
            if (objs[i].getClass() != String.class) {
                throw new RuntimeException("key必须是字符串");
            }
            i = i + 2;
        }
        return MapUtils.putAll(new HashMap<String, Object>(), objs);
    }

    @SuppressWarnings("unchecked")
    public static Map<String, String> strStr(String... objs) {
        if (objs.length % 2 != 0) {
            throw new RuntimeException("长度必须是2的倍数");
        }
        return MapUtils.putAll(new HashMap<String, String>(), objs);
    }

    public static String genSignStr(Map<String, String> params) {
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);
        String prestr = "";
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            if (i == keys.size() - 1) {//拼接时，不包括最后一个&字符
                prestr += key + "=" + value;
            } else {
                prestr += key + "=" + value + "&";
            }
        }
        return prestr;
    }

    /**
     * 对象转Map
     * object 的field 作为key ,object的field的值作为value
     *
     * @param object
     * @return
     */
    public static Map<String, Object> beanToMap(Object object) {
        if (object == null) {
            throw new NullPointerException();
        }
        Map<String, Object> map = new HashMap<>();
        Class<?> objectClass = object.getClass();
        Field[] declaredFields = objectClass.getDeclaredFields();
        if (declaredFields != null) {
            for (Field declaredField : declaredFields) {
                try {
                    // 静态变量忽略
                    boolean isStatic = Modifier.isStatic(declaredField.getModifiers());
                    if (isStatic) {
                        continue;
                    }
                    declaredField.setAccessible(true);
                    Object o = declaredField.get(object);
                    if (o != null) {
                        map.put(declaredField.getName(), o);
                    }
                } catch (IllegalAccessException ignore) {
                    ignore.printStackTrace();
                }
            }
        }
        return map;
    }

    public static <T> List<Map<String, Object>> beanToMap(List<T> objects) {
        List<Map<String,Object>> list = new ArrayList<>();
        for (Object object : objects) {
            Map<String,Object> map = beanToMap(object);
            list.add(map);
        }
        return list;
    }
    
}
